var app = new Vue({
    el: "#app",
    data: {
        entity: {},
        categoryList: [],
        category2List: [],
        category1Id: 0,
        flag:false
    },
    methods: {
        //查询所有的一级分类
        findAllCategory: function () {
            axios.get("../category/findAll.do").then(function (response) {
                app.categoryList = response.data;
            });
        },

        save: function () {
            //设置商品描述信息
            this.entity.article = editor.html();
            var method = "save";
            if (this.entity.id != null) {
                //如果id存在则说明是 修改
                method = "update"
            }

            axios.post("../article/" + method + ".do", this.entity).then(function (response) {
                if (response.data.success) {
                    alert(response.data.message);
                    editor.html("");
                    location.href = "content.html";
                } else {
                    alert(response.data.message);
                }
            });

        },

        findOne: function () {
            var id = this.getParameterByName("id");
            if (id == null) {
                return;
            }
            axios.get("../article/findOne/" + id + ".do").then(function (response) {
                app.entity = response.data;
                editor.html(app.entity.article);
                app.flag = true
            });

        },

        //根据参数名字获取参数
        getParameterByName: function (name) {
            return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
        },


    },
    watch: {
        category1Id: {
            handler: function (newValue, oldValue) {
                for (var cate of this.categoryList) {
                    if (this.category1Id == cate.id) {
                        this.category2List = cate.categoryList;
                        return;
                    }
                }
            }
        },
        flag:{
            handler: function () {
                axios.get("../category/findCategoryById/" + this.entity.categoryId + ".do").then(function (response) {
                    app.category1Id = response.data.id;
                });

            }
        }
    },
    created: function () {
        this.findAllCategory();

        this.findOne();
    }
});